var app = getApp()
//常量数据配置
var ConstantData = require('../../common/constant_data.js')
var common = require("../../common/common.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    deposits:[],
    callServiceBoxStatus:false,
    refundToPay:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.depositList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.depositList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  toPayDeposit:function(event) {
    var eqtid = 0
    var isrefund = 0
    if(event) {
      console.log("去支付押金。。。。" + JSON.stringify(event.currentTarget))
      console.log("header=" + JSON.stringify(app.getHeader()))
      console.log("-------eqtid=" + event.currentTarget.dataset.eqtid)
      eqtid = event.currentTarget.dataset.eqtid
      isrefund = event.currentTarget.dataset.isrefund;
    }
   
    if (this.data.refundToPay) {
      console.log("去取消退押金申请")
      eqtid = this.data.refundEqTid
    }else {
      if (isrefund == 1) {
        this.setData({
          callServiceBoxStatus: true,
          refundEqTid: eqtid
        })
        return
      }
    }
    if(eqtid == 0) {
      wx.showToast({
        title: '参数有误'
      })
      return
    }
    
    var that = this

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_dposit/wxmini/to_pay_d',
        header: header,
        data: {
          eqTid: eqtid
        },
        success: function (res) {
          var data = res.data
          console.log("押金支付结果")
          console.log(data);
          if (data.result == 0) {
            var orderCode = data.data
            wx.request({
              url: ConstantData.CONSTATNT_DATA.BASE_URL + '/ejiam/wxmini/unpay/payment',
              header: header,
              data: {
                ordernum: orderCode
              },
              success: function (res) {
                var data = res.data
                if (data.result == 0) {//跳列表页
                  if (data.data.orderType == 30) { //押金列表
                    wx.navigateTo({
                      url: '../user_record/deposit/user_deposit',
                    })
                  } else if (data.data.orderType == 31) {//租赁记录列表
                    wx.navigateTo({
                      url: '../user_record/lease/user_lease',
                    })
                  }
                } else if (data.result == -1) {//去支付
                  var orderCode = data.data.orderCode
                  that.toPay(orderCode)
                } else {//错误
                  wx.showToast({
                    title: data.description
                  })
                }
              }
            })
          } else if (data.result == -1) {//已取消退款申请
            console.log("已取消退款申请")
            that.setData({
              refundToPay: false
            })
            wx.navigateBack({
              delta: 3
            })
          } else {
            wx.showToast({
              title: data.description
            })
          }
        },
        fail: function (res) {
          console.log("去支付押金失败")
          console.log(res)
        }
      })

    })
    
  },
  depositList:function() {
    var that = this

    app.executeRequest(function (header) {

      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_dposit/wxmini/info',
        header: header,
        success: function (res) {
          var data = res.data
          that.setData({
            deposits: data
          })
        },
        fail: function (res) {
          console.log("租赁押金列表访问失败")
          console.log(res)
        }
      })

    })
    
  },
  toPay: function (orderCode) {
    var that = this
    console.log("去支付。。。" + orderCode)
    app.toPayValideLogin(orderCode, function(loginCode, orderCode, err){

      app.executeRequest(function (header) {

        wx.request({
          url: ConstantData.CONSTATNT_DATA.BASE_URL + '/ejiam/wxmini/weixin/pay',
          header: header,
          data: {
            orderCode: orderCode,
            loginCode: loginCode,
            wxErr: err
          },
          success: function (res) {
            var data = res.data
            console.log("支付结果---" + data.result)
            if (data.result != 0 || !data.data || !data.data.timeStamp) {
              wx.showToast({
                title: data.description ? data.description:'支付失败'
              })
              return
            }
            wx.requestPayment({
              appId: 'wx0e458dc331e557cc',
              timeStamp: data.data.timeStamp,
              nonceStr: data.data.nonceStr,
              package: data.data.package,
              signType: data.data.signType,
              paySign: data.data.paySign,
              success: function (res) {
                console.log("SUCCESS")
                console.log(res)
                // if (wx.requestSubscribeMessage) {
                //   wx.requestSubscribeMessage({
                //     tmplIds: ['KnUsFuS2NB07Pf9xbznQlyK_qZOU7DxOFl8l6E26Nww', '-S91jtVpX2Vto5EyXauKW008LYSSbsZiI8IWdepDarE'],
                //     success(res) {
                //       wx.request({
                //         url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_user/wx_sub_msg',
                //         header: header
                //       })}
                //   })
                // } 
                wx.navigateBack({
                  delta: 3
                })
              },
              fail: function (res) {
                common.recordErrorInfo(ConstantData.err_type.pay_fail, res ? 'dep-' + JSON.stringify(res) : '');
              },
              complete: function (res) {
                console.log("complete--" + data.data.paySign)
                console.log(res)
              }
            })
          }
        })
  
      })

    });

    
  },
  cancleApply:function() {
    this.setData({
      callServiceBoxStatus:false,
      refundToPay: false
    })
  },
  sureApply:function() {
    this.setData({
      callServiceBoxStatus: false,
      refundToPay:true
    })
    this.toPayDeposit()
  }
})